﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Person;
using BusinessObjects.Person;
using System.Data;
using DataObjects.AdoNet;

namespace DataObjects.SqlServer.Person
{
    public class SqlServerProvincesDao : IProvincesDao
    {
        public List<Province> GetProvinces()
        {
            string procedures = "Get_All_Provinces";

            return Db.ReadList(procedures, true, Make, null);
        }

        public Province GetProvince(string ProvinceCode)
        {
            string procedures = "Get_Province_ByCode";

            object[] parms = { "@ProvinceCode", ProvinceCode };
            return Db.Read(procedures, true, Make, parms);
        }

        public List<Province> GetProvincesByIsActive(bool isActive)
        {
            string procedures = "Get_All_Provinces_ByIsActive";

            object[] parms = { "@IsActive", isActive };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<Province> GetProvincesByPage(int currentPage)
        {
            string procedures = "Get_Paged_Provinces";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_Province";

            return Db.ReadTotalPage(procedures, true, null);
        }

        public void InsertProvince(Province Province)
        {
            string sql = "Insert_Province";

            Province.ProvinceCode = Db.Insert(sql, true, Take(Province));
        }

        public void UpdateProvince(Province Province)
        {
            string sql = "Update_Province";

            Db.Update(sql, true, Take(Province));
        }

        public void DeleteProvince(Province Province)
        {
            string sql = "Delete_Province";

            Db.Update(sql, true, Take(Province.ProvinceCode));
        }

        private object[] Take(string ProvinceCode)
        {
            return new object[]  
            {
                "@ProvinceCode", ProvinceCode,
            };
        }

        private object[] Take(Province Province)
        {
            return new object[]  
            {
                "@ProvinceCode", Province.ProvinceCode,
                "@ProvinceName", Province.ProvinceName,
                "@CreateDate", Province.CreateDate,
                "@ModifiedDate", Province.ModifiedDate,
                "@Description", Province.Description,
                "@IsActive", Province.IsActive
            };
        }

        private static Func<IDataReader, Province> Make = reader =>
            new Province
            {
                ProvinceCode = reader["ProvinceCode"].AsString(),
                ProvinceName = reader["ProvinceName"].AsString(),
                CreateDate = reader["CreateDate"].AsDateTime(),
                ModifiedDate = reader["ModifiedDate"].AsDateTime(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
    }
}
